package com.cloudptt.wp.ui;

import java.util.ArrayList;
import java.util.List;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.address.adapter.AddrListAdapter;
import com.cloudptt.wp.ui.message.SessionActivity;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.HistoryInfo;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

public class ContactFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener,OnItemLongClickListener,DialogInterface.OnClickListener{

	private static String TAG = "ContactFragment";

	//	private SwipeRefreshLayout swipeRefreshLayout;
	private ListView listView;
	private AddrListAdapter adapter;
	//	private Context mContext;
	private List<AddrInfo> addrList = new ArrayList<AddrInfo>();//getTestInfo();
	private Context ctx = null;
	private AddrInfo mcIdInfo = null;

	private View mRootView;

	private LinearLayout mEmptyLayout;
	private IAddrUpdate update = null;
	private int curGroupPos = 0;
	//	private GroupInfo defaultGroupInfo = null;
//	private RollbackTimer rollbackTimer = null;
	private Product product;

	public ContactFragment() {
	}

	public ContactFragment(Context ctx,AddrInfo mcIdInfo,IAddrUpdate update){
		this.ctx = ctx;
		this.mcIdInfo = mcIdInfo;
		this.update = update;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if(mRootView != null){
			return mRootView;
		}
		mRootView = inflater.inflate(R.layout.contact_fragment, container, false);
//		swipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.main_srl);
		listView = (ListView) mRootView.findViewById(R.id.addr_list);
		if(update != null)
		{
			AddrInfo curGroupInfo = update.getCurGroupInfo();
			Log.d(TAG, "onCreateView curGroupInfo = "+curGroupInfo);
			adapter = new AddrListAdapter(getActivity(), addrList,mcIdInfo);
		}

		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);

		mEmptyLayout = (LinearLayout)mRootView.findViewById(R.id.empty_ll);
//		mEmptyLayout.setOnClickListener(this);

//		if(addrList != null
//				&& addrList.size() > 0){
//
//			mEmptyLayout.setVisibility(View.INVISIBLE);
//		}else{
//
//			mEmptyLayout.setVisibility(View.VISIBLE);
//		}

//		swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
//				android.R.color.holo_orange_light, android.R.color.holo_red_light);
//
//		swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//			@Override
//			public void onRefresh() {
//				if(update != null)
//				{
////					update.loadAddrList();
//					update.loadAddressAndOnline();
//				}
//				rollbackTimer = new RollbackTimer(ctx,3,ContactFragment.this);
//			}
//		});

		return mRootView;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		updateAddrList(false);
//		updateData(groupList);
//		if(exPandableListView != null)
//		{
//			exPandableListView.expandGroup(0);
//		}
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	public void updateAddrList(boolean isNotify){
		if(product == null
				&& ctx != null){
			SysApplication app = ((SysApplication) ctx.getApplicationContext());
			this.product = app.getProduct();
		}
		if(product!=null){
			AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
			List<AddrInfo> list = operator.getAddrInfoList();
			Log.d(TAG, "updateAddrList list = " + list);
			updateData(isNotify,list);
		}
	}

	private void updateData(boolean isNotify,List<AddrInfo> addrList)
	{
//		handler.sendEmptyMessage(0x101);//通过handler发送一个更新数据的标记

		if(addrList == null){
			return;
		}
		Log.d(TAG, "updateData addrList = "+addrList);
		this.addrList.clear();
		this.addrList.addAll(addrList);
		if(this.adapter != null)
		{
			this.adapter.refresh(this.addrList);
		}

		if(mEmptyLayout == null){
			return;
		}

		if(this.addrList != null
				&& this.addrList.size() > 0){

			mEmptyLayout.setVisibility(View.INVISIBLE);
		}else{
			if(isNotify)
			{
				mEmptyLayout.setVisibility(View.INVISIBLE);
			}
			else {
				mEmptyLayout.setVisibility(View.VISIBLE);
			}
		}
	}


	public void setMyLoginInfo(AddrInfo myInfo){
		this.mcIdInfo = myInfo;
		if(adapter !=null){
			adapter.setMyLoginInfo(myInfo);
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		final AddrInfo info = (AddrInfo)listView.getAdapter().getItem(position);
		goToMsgActivity(info);
	}
	private void goToMsgActivity(AddrInfo addrInfo)
	{
		Log.d(TAG, "goToMsgActivity addrInfo = " + addrInfo);
		//		AddrInfo addrInfo = groupInfo.getUserList().get(childPosition);
		Intent intent = new Intent(ctx, SessionActivity.class);
		intent.putExtra(SessionActivity.KEY, addrInfo);
		ctx.startActivity(intent);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		Log.d(TAG, "updateData position = "+ position);
		curGroupPos = position;
		Log.d(TAG, "updateData curGroupPos = "+ curGroupPos);
		final AddrInfo info = (AddrInfo)listView.getAdapter().getItem(position);
		Log.d(TAG, "updateData info = "+ info);
		if (info.isGroupType())
		{
			String item1 = ctx.getString(R.string.quick_group);
//			String item2 = ctx.getString(R.string.make_conference);
//			String item3 = ctx.getString(R.string.new_group);
//			String item4 = ctx.getString(R.string.del_group);
			String[] list = new String[]{item1};
			UiDlg.showListDialog(ctx, list, this);

		}
		return true;
	}

	@Override
	public void onClick(DialogInterface arg0, int index) {
		// TODO Auto-generated method stub
		AddrInfo info = (AddrInfo)listView.getAdapter().getItem(curGroupPos);
		Log.d(TAG, "updateData info = "+info);
		if(info != null)
		{
			SysApplication app = ((SysApplication) ctx.getApplicationContext());
			Product product = app.getProduct();

			if(product!=null)
			{
				if(index == 0)
				{
					if(update != null)
					{
						update.setDefaultGroupInfo(info);
						updateAddrList(false);
//						update.loadAddressAndOnline();
//						update.loadAddrList();
					}
				}
//				else if(index == 1)
//				{
////					product.getPCallEventAdapter().makePrivateCall(info, CallAttr.CallType_Conference,false);
//				}
//				else if(index == 2)
//				{
//					Intent intent = new Intent(ctx,NewGroupActivity.class);
//					startActivity(intent);
//					//overridePendingTransition( R.anim.in_from_left, R.anim.out_to_right );
//				}
//				else if(index == 3)
//				{
//					if(info.isSameOne(GroupInfo.Group_Num_All))
//					{
//						CommDlg.show(ctx, ctx.getResources().getString(R.string.group_del_group_all));
//						return;
//					}
//					String curMcId = mcIdInfo.getMcId();
//					StringBuffer strBuf = new StringBuffer(curMcId);
//					List<UserInfo> userList = info.getUserList();
//					if(userList == null
//							|| userList.size() <= 0)
//					{
//						CommDlg.show(ctx, ctx.getResources().getString(R.string.group_memeber_null_1));
//						return;
//					}
//
//					for(Iterator<UserInfo> itor = userList.iterator();itor.hasNext();)
//					{
//						UserInfo userInfo = itor.next();
//						if(userInfo != null
//								&& !userInfo.isSameOne(curMcId))
//						{
//							String mcId = userInfo.getMcId();
//							strBuf.append("|").append(mcId);
//						}
//					}
//					SysMsgOperator op = (SysMsgOperator)product.getOperator(ProductConst.Func_SysMsg);
//					op.sendSelfDelGroupMsg(info.getMcId(),strBuf.toString());
//					this.rollbackTimer = new RollbackTimer(ctx,3,this);
//				}
			}

		}
	}


//	private Handler handler = new Handler(){
//		@Override
//		public void handleMessage(Message msg) {
//			super.handleMessage(msg);
//			switch (msg.what){
//			case 0x101:
//				if (swipeRefreshLayout != null
//						&& adapter != null
//						&& swipeRefreshLayout.isRefreshing())
//				{
//					adapter.notifyDataSetChanged();
//					swipeRefreshLayout.setRefreshing(false);//设置不刷�?
//				}
//				if(rollbackTimer != null)
//				{
//					rollbackTimer.cancelTimer();
//					rollbackTimer = null;
//				}
//				break;
//			}
//		}
//	};

//	@Override
//	public void rollback() {
//		// TODO Auto-generated method stub
//		handler.sendEmptyMessage(0x101);//通过handler发送一个更新数据的标记
//	}

}
